package mysql

import (
	"furnipack/furnipackrpc/furnipackrpc"
	"gorm.io/gorm"
)

type Furn struct {
	gorm.Model
	TitleId int    `gorm:"type:int;comment:品牌"`
	Name    string `gorm:"type:varchar(30);comment:名称"`
	TyperId int    `gorm:"type:int;comment:类型ID"`
}

type FurnData struct {
	TitleName string
	Name      string
	TyperName string
}

func (f *Furn) FurnAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}
func FurnFind(db *gorm.DB, in *furnipackrpc.FurnShowRequest) (FurnData, error) {

	var furnData FurnData

	err2 := db.Debug().Model(&Furn{}).Select(
		"titlers.title_name",
		"furns.`name` ",
		"typers.typer_name ",
	).
		Joins("LEFT JOIN typers ON furns.typer_id = typers.id").
		Joins("LEFT JOIN titlers ON furns.title_id = titlers.id").
		Where("furns.id=?", in.FurnId).
		Find(&furnData).Error

	return furnData, err2
}
func FurnList(db *gorm.DB) ([]*furnipackrpc.FurnList, error) {
	var list []*furnipackrpc.FurnList
	//SELECT
	//  titlers.title_name as Title,
	//  furns.`name` ,
	//  typers.typer_name as Typer
	//FROM
	//  furns
	//  LEFT JOIN typers ON furns.typer_id = typers.id
	//  LEFT JOIN titlers ON furns.title_id = titlers.id
	err2 := db.Debug().Model(&Furn{}).Select(
		"furns.id as fruns_id",
		"titlers.title_name",
		"furns.`name`",
		"typers.typer_name",
	).Joins("LEFT JOIN typers ON furns.typer_id = typers.id").
		Joins("LEFT JOIN titlers ON furns.title_id = titlers.id").
		Find(&list).Error
	return list, err2
}
